package cn.team11.sds.dayi;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.util.Log;

import cn.team11.sds.dayi.debugpage.WelcomeActivity;
import cn.team11.util.T11Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.taobao.agoo.TaobaoBaseIntentService;

import org.android.agoo.common.AgooConstants;

import java.util.concurrent.atomic.AtomicInteger;

public class TaobaoIntentService extends TaobaoBaseIntentService {
    private final static String TAG = "TaobaoIntentService";

    static AtomicInteger notificationIndex = new AtomicInteger(1);
    
    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "onCreate()");
    }

    @Override
    protected void onError(Context context, String errorId) {
        Log.e(TAG, "onError: " + errorId);
    }

    @Override
    protected void onRegistered(Context context, String registrationId) {
        Log.e(TAG, "onRegistered: " + registrationId);
    }

    @Override
    protected void onUnregistered(Context context, String registrationId) {
        Log.e(TAG, "onUnregistered: " + registrationId);
    }

    @Override
    protected void onMessage(final Context context, final Intent intent) {
        T11Log.d("onMessage start");
        String message = (null != intent ? intent.getStringExtra(AgooConstants.MESSAGE_BODY) : "");

        Log.e(TAG, "onMessage(): [" + message + "]");
        if (TextUtils.isEmpty(message)) {
            return;
        }


        try{
            JSONObject jsonObject = JSON.parseObject(message);

            String title = jsonObject.getString("title");
            String ticker = jsonObject.getString("ticker");
            String text = jsonObject.getString("text");
            title = "门锁管家";
            ticker = title;
            T11Log.d("接收到的推送消息："+message);

//            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
//            builder.setSmallIcon(R.mipmap.small_icon);
//            builder.setContentTitle(title);
//            builder.setTicker(ticker);
//            builder.setContentText(text);
//            builder.setDefaults(Notification.DEFAULT_ALL);
//
//
//            Intent intent1 = new Intent(this,WelcomeActivity.class);
//            PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent1,PendingIntent.FLAG_UPDATE_CURRENT);
//            builder.setContentIntent(pendingIntent);
//
//
//            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//            notificationManager.notify(notificationIndex.getAndIncrement(), builder.build());

            Intent intent1 = new Intent(this,WelcomeActivity.class);

            NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            //实例化通知
            NotificationCompat.Builder builder=new NotificationCompat.Builder(this);

            builder.setContentTitle(title);//设置通知标题
            builder.setContentText(text);//设置通知内容
            builder.setTicker(ticker);
            builder.setDefaults(NotificationCompat.DEFAULT_ALL);//设置通知的方式，震动、LED灯、音乐等
            builder.setAutoCancel(true);//点击通知后，状态栏自动删除通知
            builder.setSmallIcon(R.mipmap.small_icon);//设置小图标
            //设置点击通知后将要启动的程序组件对应的PendingIntent
//            builder.setContentIntent(PendingIntent.getActivity(this,0x102,intent1,PendingIntent.FLAG_UPDATE_CURRENT));
            builder.setContentIntent(PendingIntent.getActivity(this,0,intent1,PendingIntent.FLAG_UPDATE_CURRENT));
            Notification notification=builder.build();

            //发送通知
//            notificationManager.notify(0x101,notification);

            notificationManager.notify(notificationIndex.getAndIncrement(),notification);

        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
